package com.qianguo.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;

import com.eva.androidlibrary.widget.TimerWidget;
import com.eva.androidlibrary.widget.mdedittext.MaterialEditText;
import com.qianguo.shop.R;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.base.BaseTitleActivity;
import com.qianguo.shop.constant.SMSType;
import com.eva.androidlibrary.api.entity.ResultData;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 找回密码
 *
 * @author eva
 * @date 16/3/9
 */
@EActivity(R.layout.activity_find_password)
public class FindPasswordActivity extends BaseTitleActivity {

    @ViewById(R.id.find_password_mobile_edit_text)
    MaterialEditText mEdtMobile;

    @ViewById(R.id.find_password_auth_code_edit_text)
    MaterialEditText mEdtAuthCode;

    @ViewById(R.id.find_password_timer_widget)
    TimerWidget mTimer;

    @ViewById(R.id.find_password_next_button)
    Button mBtnSubmit;

    public static void jumpToFindPsw(Context context) {
        Intent intent = new Intent(context, com.qianguo.shop.activity.FindPasswordActivity_.class);
        context.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.find_password_title);
    }

    @Override
    protected void initializeUI() {
        mTimer.setOnTimerListener(new TimerWidget.OnTimerListener() {
            @Override
            public void onClick() {
                String mobile = mEdtMobile.getText().toString();
                if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
                    mEdtMobile.setError(getString(R.string.edit_error_mobile));
                } else {
                    getAuthCode(mobile);
                }
            }

            @Override
            public void onTiming(int remainTime) {

            }

            @Override
            public void onFinish() {

            }
        });

    }

    /**
     * 下一步
     */
    @Click(R.id.find_password_next_button)
    void clickNext() {
        String mobile = mEdtMobile.getText().toString();
        String authCode = mEdtAuthCode.getText().toString();
        if (TextUtils.isEmpty(authCode)) {
            mEdtAuthCode.setError(getString(R.string.edit_error_auth_code));
        } else {
            checkAuthCode(mobile, authCode);
        }
    }

    /**
     * 后台请求发送短信验证码
     *
     * @param mobile
     */
    @Background
    void getAuthCode(String mobile) {
        mApp.getApi().sendSMS(SMSType.TYPE_FIND_PWD, mobile, "", new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(FindPasswordActivity.this, msg);

            }
        });
    }

    /**
     * 后台请求找回密码
     *
     * @param mobile
     * @param authCode
     */
    @Background
    void checkAuthCode(final String mobile, final String authCode) {
        mApp.getApi().checkAuthCode(SMSType.TYPE_FIND_PWD, mobile, authCode, "", new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {
                FindPasswordStep2Activity.jumpToFindPswStep2(FindPasswordActivity.this, mobile, authCode);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(FindPasswordActivity.this, msg);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            this.finish();
        }
    }
}
